<?php

/*
	info: 网站首页
	date: 2011-12-12
*/

if (isset($_GET['action']) && $_GET['action'] == 'article') {
	define('USER_LOGIN_CHECK', TRUE);
}
//define('USER_LOGIN_CHECK', TRUE);

require dirname(__FILE__).'/include/common.inc.php';

$webglobal['file_news'] = array();

//获取站点公告
global_ConnectDB();

$newsCategoryDataModel = CategoryDataModel::factory('news', array('cache' => true));

if ($webglobal['action'] == 'type') {
	$webrequest['id'] = isset($webrequest['id']) ? intval($webrequest['id']) : 0;
	if (!$category = $newsCategoryDataModel->getItem($webrequest['id'])) {
		global_ShowMessage('对不起,没有该分类', '', 'BACK');
	}

	$webglobal['htmltitle'] = $category['seotitle'];
	$webglobal['htmlkeyword'][] = $category['keywords'];
	$webglobal['htmldescription'] = $category['description'];
	$webglobal['file_news']['category'] = $category;


	//封面频道
	if ($category['ispart']) {
		define('PAGESCRIPT', 'news_type');

		$webglobal['file_news']['childCategory'] = array();
		if ($childCategory = $newsCategoryDataModel->getChildForLayer($webrequest['id'], 1, true)) {
			foreach ($childCategory as $child) {
				$child['typeurl'] = WEBURLPATH . '/news.php?action=type&id=' . $child['id'];
				$child['typename'] = $child['name'];
				$child['checked'] = $child['id'] == $webrequest['id'] ? true : false;
				$webglobal['file_news']['childCategory'][] = $child;
			}
		}
		//查询幻灯片
		$flashNewsCondition = array(
			'flag' => 'f',
			'limit' => '5',
			'typeid' => $webrequest['id'],
		);
		$webglobal['file_news']['flashnews'] = getArcList($flashNewsCondition);

		//普通文章
		if ($category['channeltype'] == 1) {
			//头条新闻
			$onenews = array(
				'flag' => 'h',
				'limit' => 1,
				'typeid' => $webrequest['id'],
				'infolen' => 100
			);
			$webglobal['file_news']['onenews'] = getArcList($onenews);

			$fournews = array(
				'flag' => 'c',
				'limit' => '1, 4',
				'typeid' => $webrequest['id'],
			);
			$webglobal['file_news']['fournews'] = getArcList($fournews);
			
			//滚动图集
			$imgscroll = array(
				'row' => 5,
				'orderby'=>'pubdate',
				'listtype'=> 'image.',
				'imgwidth' => 124,
				'imgheight' => 94,
				'typeid' => $webrequest['id'],
			);
			$webglobal['file_news']['imgscroll'] = getArcList($imgscroll);

			//获取推荐
			$commend = array(
				'flag' => 'c',
				'row' => 6,
				'titlelen' => 42,
				'orderby' => 'pubdate',
				'infolen' => 80,
				'typeid' => $webrequest['id'],
			);
			$webglobal['file_news']['commend'] = getArcList($commend);

			$hot = array(
				'orderby' => 'click',
				'row' => 10,
				'typeid' => $webrequest['id'],
			);
			$webglobal['file_news']['hot'] = getArcList($hot);

			//获取顶级分类
			$webglobal['file_news']['channelartlist'] = array();
			if ($rootTypes = $newsCategoryDataModel->getChildForLayer($webrequest['id'], 1, false)) {
				foreach ($rootTypes as &$type) {
					$channelList = array(
						'titlelen' => '60',
						'row'   => 8,
						'typeid' => $type['id']
					);
					$type['typeurl'] = WEBURLPATH . '/news.php?action=type&id=' . $type['id'];
					$type['typename'] = $type['name'];
					$type['list'] = array();
					$type['list'] = getArcList($channelList);
					$webglobal['file_news']['channelartlist'][] = $type;
				}
			}

			template_Display('news_index_article');
		//图片集
		} elseif ($category['channeltype'] == 2) {
			//头条新闻
			$onenews = array(
				'flag' => 'c',
				'limit' => 1,
				'typeid' => $webrequest['id'],
				'infolen' => 150
			);
			$webglobal['file_news']['onenews'] = getArcList($onenews);

			$fournews = array(
				'flag' => 'c',
				'limit' => '1, 4',
				'typeid' => $webrequest['id'],
			);
			$webglobal['file_news']['fournews'] = getArcList($fournews);

			$webglobal['file_news']['channelartlist'] = array();
			if ($rootTypes = $newsCategoryDataModel->getChildForLayer($webrequest['id'], 1, false)) {
				foreach ($rootTypes as &$type) {
					$imageList = array(
						'row'   => 3,
						'typeid' => $type['id'],
						'listtype'=> 'image.',
					);
					$type['typeurl'] = WEBURLPATH . '/news.php?action=type&id=' . $type['id'];
					$type['typename'] = $type['name'];
					$type['image'] = array();
					$type['image'] = getArcList($imageList);

					$articleList = array(
						'titlelen' => '60',
						'row'   => 5,
						'typeid' => $type['id']
					);

					$type['article'] = array();
					$type['article'] = getArcList($articleList);

					$webglobal['file_news']['channelartlist'][] = $type;
				}
			}
			
			$hot = array(
				'orderby' => 'click',
				'row' => 5,
				'typeid' => $webrequest['id'],
				'listtype'=> 'image.',
			);
			$webglobal['file_news']['hot'] = getArcList($hot);

			//获取推荐
			$commend = array(
				'row' => 8,
				'listtype'=> 'image.commend.',
				'typeid' => $webrequest['id'],
			);

			$webglobal['file_news']['commend'] = getArcList($commend);

			template_Display('news_index_image');
		}

	//列表频道
	} else {
		$webglobal['file_news']['childCategory'] = array();
		if ($childCategory = $newsCategoryDataModel->getChildForLayer($category['upid'], 1, false)) {
			foreach ($childCategory as $child) {
				$child['typeurl'] = WEBURLPATH . '/news.php?action=type&id=' . $child['id'];
				$child['typename'] = $child['name'];
				$child['checked'] = $child['id'] == $webrequest['id'] ? true : false;
				$webglobal['file_news']['childCategory'][] = $child;
			}
		}

		//获取位置
		$webglobal['file_news']['position'] = '';
		$position = array();
		
		if (isset($category['upCategoryID']) && $category['upCategoryID']) {
			foreach ($category['upCategoryID'] as $parentID) {
				if ($parentID == 0) {
					$position[] = '<a href="'.WEBURLPATH.'">主页</a>';
				} else {
					$parentCategory = $newsCategoryDataModel->getItem($parentID);
					$position[] = '<a href="'.WEBURLPATH.'/news.php?action=type&id='.$parentID.'">'.$parentCategory['name'].'</a>';
				}
			}
			$position[] = '<a href="'.WEBURLPATH.'/news.php?action=type&id='.$webrequest['id'].'">'.$category['name'].'</a>';
		}
		$webglobal['file_news']['position'] = implode(' > ', $position);
		$webrequest['page'] = isset($webrequest['page']) ? max(1, intval($webrequest['page'])) : 1;

		$filter = array();
		if ($typeids = $newsCategoryDataModel->getChildIds($webrequest['id'], 0, true)) {
			$filter[] = "arc.news_category_id IN (". implode(',', $typeids) .")";
		}
		$filter[] = "arc.arcrank>-1";

		$webglobal['file_news']['perpage'] = 10;

		$countSql = "SELECT COUNT(*) FROM {$webconfig['dbpre']}news arc";
		$countSql .= $filter ? ' WHERE ' . implode(' AND ', $filter) : '';
		$webglobal['file_news']['datanum'] = $db->GetNum($countSql);

		$webglobal['file_news']['dataarray'] = array();
		if ($webglobal['file_news']['datanum'] > 0) {
			$option = array(
				'action' => 'type',
				'id' => $webrequest['id'],
				'page' => '@@'
			);
			$webglobal['page'] = global_Page($webglobal['file_news']['datanum'], $webglobal['file_news']['perpage'], $webrequest['page'], buildURLForModule('news', $option));

			$tempstartnum = ($webrequest['page']-1)*$webglobal['file_news']['perpage'];

			$sql = "SELECT arc.*,tp.id as tp_id, tp.name as tp_name, tp.seotitle as tp_seotitle, tp.keywords as tp_keywords  FROM {$webconfig['dbpre']}news arc left join {$webconfig['dbpre']}news_category tp on arc.news_category_id=tp.id";
			$sql .= $filter ? ' WHERE ' . implode(' AND ', $filter) : '';
			$sql .= " ORDER BY arc.sortrank DESC";
			$sql .= " LIMIT $tempstartnum, {$webglobal['file_news']['perpage']}";

			$autoindex = 0;
			$query = $db->Query($sql);
			while ($row = $db->FetchArray($query)) {
				$autoindex++;
				$row['info'] = $row['infos'] = global_CutStr($row['description'], 300);
				$row['title'] = SpHtml2Text($row['title']);
				$row['filename'] = $row['arcurl'] = WEBURLPATH . '/news.php?action=article&id=' . $row['id'];

				$row['typeurl'] = WEBURLPATH . '/news.php?action=type&id=' . $row['tp_id'];

				if($row['litpic'] == '-' || $row['litpic'] == '') {
					$row['litpic'] = WEBURLPATH.'/images/defaultpic.gif';
				} elseif (!preg_match("#^http:\/\/#i", $row['litpic'])) {
					$row['litpic'] = WEBURLPATH.$row['litpic'];
				}

				$row['picname'] = $row['litpic'];
				if ($category['channeltype'] == 1) {
					$row['stime'] = global_FormatTime($row['showtime']);
				} else {
					$row['stime'] = global_FormatTime($row['showtime'], 'FD');
				}
				
				$row['typelink'] = "<a href='".$row['typeurl']."'>".$row['tp_name']."</a>";
				$row['imglink'] = "<a href='".$row['filename']."'>".$row['image']."</a>";
				$row['fulltitle'] = $row['title'];
				$row['title'] = global_CutStr($row['title'], 30);
				if($row['color']!='') $row['title'] = "<font color='".$row['color']."'>".$row['title']."</font>";
				if(preg_match('#b#', $row['flag'])) $row['title'] = "<strong>".$row['title']."</strong>";
				//$row['title'] = "<b>".$row['title']."</b>";
				$row['textlink'] = "<a href='".$row['filename']."'>".$row['title']."</a>";
				$row['autoindex'] = $autoindex;
				
				$webglobal['file_news']['dataarray'][$row['id']] = $row;
			}
		}
		//获取推荐
		$commend = array(
			'flag' => 'c',
			'row' => 6,
			'titlelen' => 42,
			'orderby' => 'pubdate',
			'infolen' => 80,
			'typeid' => $webrequest['id'],
		);
		$webglobal['file_news']['commend'] = getArcList($commend);

		//普通文章
		if ($category['channeltype'] == 1) {
			define('PAGESCRIPT', 'news_list');
			$hot = array(
				'orderby' => 'click',
				'row' => 10,
				'typeid' => $webrequest['id'],
			);
			$webglobal['file_news']['hot'] = getArcList($hot);
			template_Display('news_list_article');
		//图片集
		} elseif ($category['channeltype'] == 2) {
			define('PAGESCRIPT', 'news_list_pic');
			$hot = array(
				'orderby' => 'click',
				'row' => 5,
				'typeid' => $webrequest['id'],
				'listtype'=> 'image.',
			);
			$webglobal['file_news']['hot'] = getArcList($hot);

			$commend = array(
				'row' => 8,
				'listtype'=> 'image.commend.',
				'typeid' => $webrequest['id'],
			);

			$webglobal['file_news']['commend'] = getArcList($commend);

			template_Display('news_list_image');
		}
	}
} elseif ($webglobal['action'] == 'article') {
	$webrequest['id'] = isset($webrequest['id']) && $webrequest['id'] ? intval($webrequest['id']) : 0;
	if (!$webrequest['id']) {
		global_ShowMessage('对不起,没有该文章', '', 'BACK');
	}

	$sql = "SELECT arc.*,tp.id as tp_id, tp.name as tp_name, tp.seotitle as tp_seotitle, tp.keywords as tp_keywords  FROM {$webconfig['dbpre']}news arc left join {$webconfig['dbpre']}news_category tp on arc.news_category_id=tp.id WHERE arc.id=".$webrequest['id'];

	$webglobal['file_news']['article'] = $db->GetOne($sql);
	$webglobal['htmltitle'] = $webglobal['file_news']['article']['title'];
	$webglobal['htmlkeyword'][] = $webglobal['file_news']['article']['title']['keywords'];
	$webglobal['htmldescription'] = $webglobal['file_news']['article']['title']['description'];

	if (!isset($webrequest['preview'])) {
		if ($webglobal['file_news']['article']['arcrank'] < 0) {
			global_ShowMessage('该文章还没有通过审核.', '', 'BACK');
			exit;
		} else {
			if ($webglobal['file_news']['article']['arcrank'] > 0) {
				if ($user['base_uid'] <= 0) {
					global_ShowMessage("请登录以后浏览该文章。", '', 'BACK');
					exit;
				}

				if ($webglobal['file_news']['article']['arcrank'] == 2) {
					if (($user['base_type'] == 0 && $user['status'] == 1) || $user['base_type'] == 1) {
						
					} else {
						global_ShowMessage("该文章只允许通过认证的用户浏览。", '', 'BACK');
						exit;
					}
				}
			}
			
		}
	}
	//获取位置
	$webglobal['file_news']['position'] = '';
	$webrequest['brandid'] = isset($webrequest['brandid']) && $webrequest['brandid'] ? intval($webrequest['brandid']) : 0;
	$position = array();
	
	$category = $newsCategoryDataModel->getItem($webglobal['file_news']['article']['news_category_id']);
	if ($webrequest['brandid'] > 0) {
		$brand = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}brand_info WHERE id = {$webrequest['brandid']} AND status = 1");
		$position = array(
			1 => '<a href="'.WEBURLPATH.'">主页</a>',
			2 => '<a href="'.WEBURLPATH.'/brand.php">VIP品牌</a>',
			3 => '<a href="'.WEBURLPATH.'/brand.php?action=view&id='.$brand['id'].'">'.$brand['name'].'</a>',
		);
	} else {
		if (isset($category['upCategoryID']) && $category['upCategoryID']) {
			foreach ($category['upCategoryID'] as $parentID) {
				if ($parentID == 0) {
					$position[] = '<a href="'.WEBURLPATH.'">主页</a>';
				} else {
					$parentCategory = $newsCategoryDataModel->getItem($parentID);
					$position[] = '<a href="'.WEBURLPATH.'/news.php?action=type&id='.$parentID.'">'.$parentCategory['name'].'</a>';
				}
			}
			$position[] = '<a href="'.WEBURLPATH.'/news.php?action=type&id='.$category['id'].'">'.$category['name'].'</a>';
		}
	}
	$webglobal['file_news']['position'] = implode(' > ', $position);



	$webglobal['file_news']['childCategory'] = array();
	if ($childCategory = $newsCategoryDataModel->getChildForLayer($category['upid'], 1, false)) {
		foreach ($childCategory as $child) {
			$child['typeurl'] = WEBURLPATH . '/news.php?action=type&id=' . $child['id'];
			$child['typename'] = $child['name'];
			$child['checked'] = $child['id'] == $category['id'] ? true : false;
			$webglobal['file_news']['childCategory'][] = $child;
		}
	}

	$webglobal['file_news']['article']['showtime'] = global_FormatTime($webglobal['file_news']['article']['showtime'], 'Y-m-d H:i');

	$digTotal = $webglobal['file_news']['article']['goodpost'] + $webglobal['file_news']['article']['badpost'];
	if ($digTotal > 0) {
		$webglobal['file_news']['article']['goodper'] = number_format($webglobal['file_news']['article']['goodpost'] / $digTotal,2) * 100;
		$webglobal['file_news']['article']['badper'] = number_format($webglobal['file_news']['article']['badpost'] / $digTotal,2) * 100;
	} else {
		$webglobal['file_news']['article']['goodper'] = 0;
		$webglobal['file_news']['article']['badper'] = 0;
	}

	if ($webglobal['file_news']['article']['channel'] == 1) {
		define('PAGESCRIPT', 'news_article');
		//推荐内容
		$commend = array(
			'flag' => 'c',
			'row' => 6,
			'infolen' => 80,
			'orderby' => 'pubdate',
			'typeid' => $category['id'],
		);
		$webglobal['file_news']['commend'] = getArcList($commend);

		$hot = array(
			'orderby' => 'click',
			'row' => 10,
			'typeid' => $category['id'],
		);
		$webglobal['file_news']['hot'] = getArcList($commend);

		template_Display('news_article_article');
	} elseif ($webglobal['file_news']['article']['channel'] == 2) {
		define('PAGESCRIPT', 'news_image');
		$webrequest['npage'] = isset($webrequest['pageno']) && $webrequest['pageno'] ? intval($webrequest['pageno']) : 1;

		$webglobal['file_news']['article']['imageTotle'] = 0;
		$webglobal['file_news']['article']['imageSrc'] = '';
		$webglobal['file_news']['article']['imageTitle'] = '';

		if (isset($webglobal['file_news']['article']['imagevalue']) && $webglobal['file_news']['article']['imagevalue'] != '') {
			$webglobal['file_news']['article']['imagevalue'] = unserialize($webglobal['file_news']['article']['imagevalue']);
			$webglobal['file_news']['article']['imageTotle'] = count($webglobal['file_news']['article']['imagevalue']);
			if ($webglobal['file_news']['article']['imageTotle'] > 0) {
				if ($tempImage = array_slice($webglobal['file_news']['article']['imagevalue'], $webrequest['npage'] - 1, 1)) {
					$tempImage = current($tempImage);
					$webglobal['file_news']['article']['imageSrc'] = WEBURLPATH . $tempImage['src'];
					$webglobal['file_news']['article']['imageTitle'] = $tempImage['info'];
				}
				
			}
		}

		//推荐内容
		$commend = array(
			'flag' => 'c',
			'row' => 6,
			'infolen' => 80,
			'listtype'=> 'image.commend.',
			'orderby' => 'pubdate',
			'typeid' => $category['id'],
		);
		$webglobal['file_news']['commend'] = getArcList($commend);

		$hot = array(
			'orderby' => 'click',
			'listtype'=> 'image.',
			'row' => 10,
			'typeid' => $category['id'],
		);
		$webglobal['file_news']['hot'] = getArcList($commend);

		template_Display('news_article_image');
	}

} elseif ($webglobal['action'] == 'digg') {
	$webrequest['id'] = isset($webrequest['id']) && $webrequest['id'] ? intval($webrequest['id']) : 0;
	$webrequest['type'] = isset($webrequest['type']) && $webrequest['type'] ? trim($webrequest['type']) : '';

	if (!$webrequest['id']) {
		echo "failed";
		exit;
	}

	if ($webrequest['type'] == 'good') {
		$db->Update('news', "goodpost=goodpost + 1", 'id='.$webrequest['id']);
	} elseif ($webrequest['type'] == 'bad') {
		$db->Update('news', "badpost=badpost + 1", 'id='.$webrequest['id']);
	}

	$digg = '';
	$row = $db->GetOne("SELECT goodpost,badpost FROM {$webconfig['dbpre']}news WHERE id='{$webrequest['id']}' ");
	if (!$row) {
		echo "failed";
		exit();
	}

	if($row['goodpost'] + $row['badpost'] == 0) {
		$row['goodper'] = $row['badper'] = 0;
	} else {
		$row['goodper'] = number_format($row['goodpost'] / ($row['goodpost'] + $row['badpost']), 3) * 100;
		$row['badper'] = 100 - $row['goodper'];
	}

	$row['goodper'] = trim(sprintf("%4.2f", $row['goodper']));
	$row['badper'] = trim(sprintf("%4.2f", $row['badper']));
	$digg = '<div class="diggbox digg_good" onmousemove="this.style.backgroundPosition=\'left bottom\';" onmouseout="this.style.backgroundPosition=\'left top\';" onclick="postDigg(\'good\','.$webrequest['id'].')">
            <div class="digg_act">顶一下</div>
            <div class="digg_num">('.$row['goodpost'].')</div>
            <div class="digg_percent">
                <div class="digg_percent_bar"><span style="width:'.$row['goodper'].'%"></span></div>
                <div class="digg_percent_num">'.$row['goodper'].'%</div>
            </div>
        </div>
        <div class="diggbox digg_bad" onmousemove="this.style.backgroundPosition=\'right bottom\';" onmouseout="this.style.backgroundPosition=\'right top\';" onclick="postDigg(\'bad\','.$webrequest['id'].')">
            <div class="digg_act">踩一下</div>
            <div class="digg_num">('.$row['badpost'].')</div>
            <div class="digg_percent">
                <div class="digg_percent_bar"><span style="width:'.$row['badper'].'%"></span></div>
                <div class="digg_percent_num">'.$row['badper'].'%</div>
            </div>
        </div>';
	echo $digg;
	exit;
} elseif ($webglobal['action'] == 'count') {
	$aid = isset($webrequest['aid']) && $webrequest['aid'] ? intval($webrequest['aid']) : 0;
	if ($aid > 0) {
		$db->Update('news', "click=click + 1", 'id='.$aid);
		$row = $db->GetOne("SELECT click FROM {$webconfig['dbpre']}news WHERE id={$aid} ");
		if(is_array($row)) {
			echo "document.write('".$row['click']."');\r\n";
		}
	}
	exit();
} else {
	define('PAGESCRIPT', 'news');
	//获取公告
	$notic = array(
		'row' => 5,
		'titlelen' => 30,
		'typeid' => 1
	);

	$webglobal['file_news']['notice'] = getArcList($notic);

	//查询幻灯片
	$flashNewsCondition = array(
		'flag' => 'f',
		'limit' => '5',
	);
	$webglobal['file_news']['flashnews'] = getArcList($flashNewsCondition);

	//头条新闻
	$onenews = array(
		'flag' => 'h',
		'limit' => 1,
		'infolen' => 160
	);
	$webglobal['file_news']['onenews'] = getArcList($onenews);

	$fournews = array(
		'flag' => 'h',
		'limit' => '1, 4',
	);
	$webglobal['file_news']['fournews'] = getArcList($fournews);

	$newarticle = array(
		'row' => '14',
		'titlelen' => '32',
		'noflag' => 'h'
	);
	$webglobal['file_news']['newarticle'] = getArcList($newarticle);
	//特别推荐
	$lightarticle = array(
		'flag' => 'a',
		'row' => 6,
		'orderby' => 'pubdate'
	);
	$webglobal['file_news']['lightarticle'] = getArcList($lightarticle);

	//滚动图集
	$imgscroll = array(
		'row' => 10,
		'orderby'=>'pubdate',
		'listtype'=> 'image.',
		'titlelen' => '20',
		'imgwidth' => 143,
		'imgheight' => 106,
	);
	$webglobal['file_news']['imgscroll'] = getArcList($imgscroll);

	//获取顶级分类
	$webglobal['file_news']['channelartlist'] = array();
	if ($rootTypes = $newsCategoryDataModel->getChildForLayer(0, 1)) {
		foreach ($rootTypes as &$type) {
			$channelList = array(
				'titlelen' => '60',
				'row'   => 8,
				'typeid' => $type['id']
			);
			$type['typeurl'] = WEBURLPATH . '/news.php?action=type&id=' . $type['id'];
			$type['typename'] = $type['name'];
			$type['list'] = array();
			$type['list'] = getArcList($channelList);
			$webglobal['file_news']['channelartlist'][] = $type;
		}
	}

	//获取推荐
	$commend = array(
		'flag' => 'c',
		'row' => 16,
		'titlelen' => 42,
		'orderby' => 'pubdate'
	);
	$webglobal['file_news']['commend'] = getArcList($commend);

	$hot = array(
		'orderby' => 'click',
		'row' => 6,
		'titlelen' => 42,
		'subday' => 30
	);
	$webglobal['file_news']['hot'] = getArcList($hot);
	template_Display('news_index');
}



